/*
 * This file is part of ELCube.
 *
 * ELCube is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ELCube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with ELCube.  If not, see <https://www.gnu.org/licenses/>.
 */
package cn.nkpro.elcube.docengine.datasync.defaults;

import cn.nkpro.elcube.data.elasticearch.SearchEngine;
import cn.nkpro.elcube.docengine.datasync.NkAbstractDocDataDiffedSyncAdapter;
import cn.nkpro.elcube.docengine.gen.DocDefDataSync;
import cn.nkpro.elcube.docengine.model.DocHV;
import cn.nkpro.elcube.docengine.model.es.DocExtES;
import cn.nkpro.elcube.docengine.model.es.DocHES;
import cn.nkpro.elcube.utils.BeanUtilz;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Map;

@Slf4j
@Component("NkDocESCloneDataSync")
public class NkDocESCloneDataSyncImpl extends NkAbstractDocDataDiffedSyncAdapter<String> {

    @Autowired@SuppressWarnings("all")
    private SearchEngine searchEngine;

    @Override
    public void onInsert(DocHV doc, Map<String,Map<String,Object>> list, DocDefDataSync def) {
        list.forEach((key,value)->
            searchEngine.updateBeforeCommit(
                    buildCustomES(
                            doc,
                            def,
                            key,
                            value
                    )
            )
        );
    }

    @Override
    public void onModify(DocHV doc, Map<String,Map<String,Object>> list, DocDefDataSync def) {
        this.onInsert(doc, list, def);
    }

    @Override
    public void onRemove(DocHV doc, Map<String,Map<String,Object>> list, DocDefDataSync def) {
        list.forEach((key,value)->searchEngine.deleteBeforeCommit(DocExtES.class, key));
    }

    private DocExtES buildCustomES(DocHV doc, DocDefDataSync def, String customId, Map<String,Object> value){
        Assert.isTrue(StringUtils.isNotBlank(customId),"数据同步服务["+getBeanName() + "] 主键id值不能为空");

        DocHES docHES = DocHES.from(doc);
        DocExtES docExtES  = BeanUtilz.copyFromObject(docHES, DocExtES.class);

        DocExtES customES = new JSONObject(value).toJavaObject(DocExtES.class);
        if(customES.getDocId()!=null) docExtES.setDocId(customES.getDocId());
        if(customES.getClassify()!=null) docExtES.setClassify(customES.getClassify());
        if(customES.getCreatedTime()!=null) docExtES.setCreatedTime(customES.getCreatedTime());
        if(customES.getDocDefVersion()!=null) docExtES.setDocDefVersion(customES.getDocDefVersion());
        if(customES.getDocName()!=null) docExtES.setDocName(customES.getDocName());
        if(customES.getDocNumber()!=null) docExtES.setDocNumber(customES.getDocNumber());
        if(customES.getDocState()!=null) docExtES.setDocState(customES.getDocState());
        if(customES.getDocStateDesc()!=null) docExtES.setDocStateDesc(customES.getDocStateDesc());
        if(customES.getDocType()!=null) docExtES.setDocType(customES.getDocType());
        if(customES.getDocTypeDesc()!=null) docExtES.setDocTypeDesc(customES.getDocTypeDesc());
        if(customES.getPartnerId()!=null) docExtES.setPartnerId(customES.getPartnerId());
        if(customES.getPartnerName()!=null) docExtES.setPartnerName(customES.getPartnerName());
        if(customES.getPreDocId()!=null) docExtES.setPreDocId(customES.getPreDocId());
        if(customES.getProcessInstanceId()!=null) docExtES.setProcessInstanceId(customES.getProcessInstanceId());
        if(customES.getTags()!=null) docExtES.setTags(customES.getTags());
        if(customES.getUpdatedTime()!=null) docExtES.setUpdatedTime(customES.getUpdatedTime());

        if(customES.getDynamics()!=null) docExtES.getDynamics().putAll(customES.getDynamics());

        docExtES.setCustomType(def.getTargetArgs());
        docExtES.setCustomId(customId);
        return docExtES;
    }
}
